package com.stu.manager.organization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.handler.LoginHandler;
import com.stu.manager.organization.entity.SysClassinfo;
import com.stu.manager.organization.entity.SysDepartment;
import com.stu.manager.organization.entity.SysProfessional;
import com.stu.manager.organization.entity.dto.AddDepartmentDto;
import com.stu.manager.organization.entity.dto.DepartmentDto;
import com.stu.manager.organization.entity.dto.ProfessionalDto;
import com.stu.manager.organization.mapper.SysClassinfoMapper;
import com.stu.manager.organization.mapper.SysDepartmentMapper;
import com.stu.manager.organization.service.SysProfessionalServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-02-12
 */
@RestController
@RequestMapping("/api/professional")
@CrossOrigin
public class SysProfessionalController {
    @Autowired
    private SysProfessionalServiceImpl professionalServiceImpl;
    @Autowired
    private LoginHandler loginHandler;
    @Resource
    private SysClassinfoMapper classinfoMapper;

    @PostMapping("pageProfessional/{current}/{limit}")
    @ApiOperation("模糊查询加分页展示学院")
    public Result pageProfessional(@PathVariable Long current,
                                   @PathVariable Long limit,
                                   @RequestBody(required = false) ProfessionalDto professionalDto, HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Page<SysProfessional> pageProfessional = new Page<>(current, limit);
        QueryWrapper<SysProfessional> wrapper = new QueryWrapper<>();
        String professionalName = professionalDto.getProfessionalName();
        String professionalCode = professionalDto.getProfessionalCode();
        String departmentCode = professionalDto.getDepartmentName();

        if (!StringUtils.isEmpty(professionalName)) {
            wrapper.like("professional_name", professionalName);
        }
        if (!StringUtils.isEmpty(professionalCode)) {
            wrapper.like("professional_code", professionalCode);
        }
        if (!StringUtils.isEmpty(departmentCode)) {
            wrapper.like("department_name",departmentCode);
        }

        IPage<SysProfessional> sysProfessionalIPage = professionalServiceImpl.pageProfessional(pageProfessional, wrapper);
        List<SysProfessional> records = sysProfessionalIPage.getRecords();
        long total = sysProfessionalIPage.getTotal();

        return Result.ok().data("total", total).data("records", records);
    }


    @PostMapping("addProfessional")
    @ApiOperation("添加专业")
    public Result addProfessional(@RequestBody ProfessionalDto professionalDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = professionalServiceImpl.addProfessional(professionalDto);
        return result;
    }

    @GetMapping("getProfessionalByCode/{professionalCode}")
    @ApiOperation("查询学院信息，修改回显")
    public Result getProfessionalByCode(@PathVariable String professionalCode,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        SysProfessional professionalByCode = professionalServiceImpl.getProfessionalByCode(professionalCode);
        return Result.ok().data("item", professionalByCode);
    }

    @PostMapping("updateProfessional")
    @ApiOperation("修改专业信息")
    public Result updateProfessional(@RequestBody SysProfessional sysProfessional,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = professionalServiceImpl.updateProfessional(sysProfessional);
        return result;
    }

    @DeleteMapping("delProfessional/{professionalCode}")
    @ApiOperation("删除专业信息")
    public Result delProfessional(@PathVariable String professionalCode,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        QueryWrapper<SysProfessional> wrapper = new QueryWrapper<>();
        wrapper.eq("professional_code", professionalCode);
        SysProfessional one = professionalServiceImpl.getOne(wrapper);
        String professionalName = one.getProfessionalName();

        QueryWrapper<SysClassinfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("professional_name",professionalName);
        Integer count = classinfoMapper.selectCount(wrapper1);
        if(count>0){
            return Result.error().message("当前专业有下属班级，请先删除班级！");
        }

        boolean remove = professionalServiceImpl.remove(wrapper);
        if (remove) {
            return Result.ok().message("删除成功！");
        } else {
            return Result.error().message("删除失败！");
        }
    }

}

